<?php

namespace App\Http\Controllers\Api\Room;

use App\Http\Controllers\Controller;
use App\Libs\Strategy\Context;
use App\Models\Rooms\RoomsLevel;
use Illuminate\Http\Request;

class RoomLevelController extends Controller
{
    private $room;

    public function __construct(){
        $this->room = new Context(new RoomsLevel());
    }

    public function index()
    {
        $rooms = $this->room->display(10);
        return view('room.level.index',compact('rooms'));
    }

    public function create()
    {
        return view('room.level.add');
    }

    public function edit($id)
    {
        $room = $this->room->find($id);
        return view('room.level.edit',compact('room'));
    }

    public function show($id)
    {
        if($id == 0)
        {
            return $this->success($this->room->display(0));
        }
    }

    public function store(Request $request)
    {
        $this->validate($request,['name'=>'required','sort'=>'max:65535']);
        if($res = $this->room->create($request->only('name','sort','is_show')));
        {
            return $this->success(['message'=>'添加成功']);
        };
        return $this->error('添加失败');
    }

    public function update(Request $request,$id)
    {
         $this->validate($request,['name'=>'required','sort'=>'max:65535']);
        if($res = $this->room->update($id,$request->only('name','sort','is_show')));
        {
            return $this->success(['message'=>'更新成功']);
        };
        return $this->error('更新失败');
    }


    public function destroy($id)
    {
        if($this->room->delete($id))
        {
            return $this->success(['message'=>'删除成功']);
        }
        return $this->error('删除失败');
    }
}
